﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Maticsoft.Common;
using CustomsSystem.BLL;
using CustomsSystem.Model;

namespace WEB.sheetInfo
{
    public partial class sheetView : System.Web.UI.Page
    {

        static DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.tbTimestart.Attributes.Add("onClick", "new WdatePicker()");//绑定时间控件
                this.tbTimestop.Attributes.Add("onClick", "new WdatePicker()");
                tbTimestop.Text = DateTime.Now.AddDays(1).ToShortDateString();//显示时间
                tbTimestart.Text = DateTime.Now.ToString("yyyy/MM/01");
                BindData();
            }
            else
            {
                SheetInfoManage sheetManage = new SheetInfoManage();
                //搜索分页数据
                //判断日期
                if (tbTimestart.Text != "" && tbTimestop.Text != "")
                {
                    if (Convert.ToDateTime(tbTimestart.Text) >= Convert.ToDateTime(tbTimestop.Text))
                    {
                        MessageBox.Show(this, "结束日期不能大于开始日期！");
                        return;
                    }
                }
                //判断搜索条件
                ds = sheetManage.GetView(Query());
                gvSheetInfo.DataSource = ds;
            }


        }

        private void BindData()
        {
            SheetInfoManage sheetManage = new SheetInfoManage();
            //搜索分页数据
            //判断日期
            if (tbTimestart.Text != "" && tbTimestop.Text != "")
            {
                if (Convert.ToDateTime(tbTimestart.Text) > Convert.ToDateTime(tbTimestop.Text))
                {
                    MessageBox.Show(this, "结束日期不能大于开始日期！");
                    return;
                }
            }
            //判断搜索条件
            ds = sheetManage.GetView(Query());
            gvSheetInfo.DataSource = ds;//绑定数据源
            gvSheetInfo.DataBind();//数据绑定
        }
        /// <summary>
        /// 页面跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int id = Convert.ToInt32(gvSheetInfo.DataKeys[e.NewEditIndex].Value);//获取ID
            Response.Redirect("sheetEdit.aspx?ID=" + id);//页面跳转
        }


        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData();//绑定数据
        }

        /// <summary>
        /// 根据委托单跟踪工作流
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string index = e.CommandArgument.ToString(); //那行被选中,取出选中行的索引 
            if (e.CommandName == "workflowView")//查看工作流键
            {
                int id = Convert.ToInt32(gvSheetInfo.DataKeys[int.Parse(index)].Value);//获取值
                Response.Redirect("~/workflow/workflowAppointo.aspx?ID=" + id);//页面跳转
            }
            if (e.CommandName == "sheetid")//查看委托单
            {
                int id = Convert.ToInt32(gvSheetInfo.DataKeys[int.Parse(index)].Value);
                Response.Redirect("~/Client/sheetDetails1.aspx?ID=" + id);
            }
        }

        protected void gvSheetInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                WorkflowHandleManage bll = new WorkflowHandleManage();
                int sheetID = Convert.ToInt32(e.Row.Cells[0].Text);//获取sheetid
                CustomsSystem.Model.workflow model;
                model = bll.GetModel(sheetID);

                int IntimeFlag = 0;//判断报关及时度
                int IntimeFlag2 = 0;//判断报检及时度
                //先判断是否完结
                bool isFinished = false;
                SheetInfoManage sheetBll = new SheetInfoManage();
                CustomsSystem.Model.sheetInfo sheetModel = sheetBll.GetModel(sheetID);
                //报检
                if (sheetModel.InfoWKTypeIDbg == -1 && sheetModel.InfoWKTypeIDbj != -1)
                {
                    if (sheetModel.busIDbj == 99) isFinished = true;
                }
                //报关
                else if (sheetModel.InfoWKTypeIDbg != -1 && sheetModel.InfoWKTypeIDbj == -1)
                {
                    if (sheetModel.busIDbg == 99) isFinished = true;
                }
                //双
                else if (sheetModel.InfoWKTypeIDbg != -1 && sheetModel.InfoWKTypeIDbj != -1)
                {
                    if (sheetModel.busIDbg == 99 && sheetModel.busIDbj == 99) isFinished = true;
                }

                //若未完结
                if (!isFinished)
                {
                    IntimeFlag = bll.GetIntime(sheetID, 1);//计算报关及时度
                    IntimeFlag2 = bll.GetIntime(sheetID, 2);//计算报检及时度
                }

                //报关
                Image tmp_ImageIntime = (Image)e.Row.Cells[13].FindControl("ImgIntime");//获得图片列的控件
                if (tmp_ImageIntime != null)
                {
                    if (IntimeFlag != -1)
                    {
                        string imagepath = "";
                        if (IntimeFlag == 1)
                        {
                            imagepath = "../images/status/Green.png";
                        }
                        else if (IntimeFlag == 2)
                        {
                            imagepath = "../images/status/Yellow.png";
                        }
                        else if (IntimeFlag == 3)
                        {
                            imagepath = "../images/status/Red.png";
                        }
                        tmp_ImageIntime.ImageUrl = imagepath;//显示图片
                    }
                    else
                    {
                        tmp_ImageIntime.Visible = false;
                    }
                }
                //报检
                Image tmp_ImageIntime2 = (Image)e.Row.Cells[17].FindControl("Imgtimebj");//获得图片列的控件
                if (tmp_ImageIntime2 != null)
                {
                    if (IntimeFlag2 != -1)
                    {
                        string imagepath = "";
                        if (IntimeFlag2 == 1)
                        {
                            imagepath = "../images/status/Green.png";
                        }
                        else if (IntimeFlag2 == 2)
                        {
                            imagepath = "../images/status/Yellow.png";
                        }
                        else if (IntimeFlag2 == 3)
                        {
                            imagepath = "../images/status/Red.png";
                        }
                        tmp_ImageIntime2.ImageUrl = imagepath;//显示图片
                    }
                    else
                    {
                        tmp_ImageIntime2.Visible = false;
                    }
                }

            }
        }

        /// <summary>
        /// 换页数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSheetInfo.PageIndex = e.NewPageIndex;//绑定最新页
            gvSheetInfo.DataBind();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public string Query()
        {
            string strQuery = ddlQuery.SelectedValue;
            string strSql = " 1=1 ";
            if (tbQuery.Text.Trim() != "")
            {
                switch (ddlQuery.SelectedIndex)  //根据选项传不同的SQL语句
                {

                    case 0:
                        strSql += string.Format("and {0} like '%{1}%'", strQuery, tbQuery.Text.Trim());
                        break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                        strSql += string.Format("and {0} like '%{1}%'", strQuery, tbQuery.Text.Trim());
                        break;
                    case 7:
                        if (!PageValidate.IsNumber(tbQuery.Text.Trim())) tbQuery.Text = "0";
                        strSql += string.Format(" and {0} = '{1}'", strQuery, tbQuery.Text.Trim());
                        break;
                    case 8:
                        strSql += string.Format("and {0} like '%{1}%'", strQuery, tbQuery.Text.Trim());
                        break;

                }
            }
            //日期搜索的判断
            if (tbTimestart.Text != "")
            {
                //日期截止时间为空的时候，默认为现在的时间
                if (tbTimestop.Text == "")
                {
                    DateTime timeStop = DateTime.Now;
                    strSql = strSql + " and agencyDate between '" + tbTimestart.Text.Trim() + "' and '" + timeStop + "'";
                }
                else
                {
                    strSql = strSql + " and agencyDate between '" + tbTimestart.Text.Trim() + "' and '" + tbTimestop.Text.Trim() + "'";
                }
            }
            strSql += " order by sheetID " + ddlasc.SelectedValue;
            return strSql;
        }

        protected void ddlasc_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void Del_Click(object sender, EventArgs e)
        {
            SheetInfoManage bll = new SheetInfoManage();
            BillManage billbll = new BillManage();
            DocumentManage docbll = new DocumentManage();
            
            int id = Convert.ToInt32(((Button)sender).CommandArgument.ToString());//获取id
            string bg = bll.GetList("sheetID= " + id).Tables[0].Rows[0]["busIDbg"].ToString().Trim();
            string bj = bll.GetList("sheetID= " + id).Tables[0].Rows[0]["busIDbj"].ToString().Trim ();
            
            if (bg != "" || bj != "")
            {
                MessageBox.Show(this, "已经启动工作流的单不可以删除！");
            }
            else if (billbll.GetList(" sheetID= " + id).Tables[0].Rows.Count > 0)
            {
                MessageBox.Show(this, "已经录入费用的单不可以删除！");
            }
            else if (docbll.GetList(" sheetID= " + id).Tables[0].Rows.Count > 0)
            {
                MessageBox.Show(this, "已经上传单证的单不可以删除！");
            }
            else
            {
                if (bll.Delete(id))
                {
                    MessageBox.ShowAndRedirect(this, "删除成功!", "sheetView.aspx");
                }
                else
                {
                    MessageBox.Show(this, "删除失败！");
                }
            }
        }

    }
}